<?php
if(!defined('DCS_ROOT')) {exit('Access Denied');} 

class moviepic {

	function __construct (){
		$this->url = getUrl();
		$action = $this->url['params']['1'];
		require_once(APP_PATH."model/admin/init.php" );
		if($action == 'add'){
			$this->add();
			exit();
		}elseif($action == 'edit'){
			$this->edit();
			exit();
		}elseif($action == 'upload'){
			$this->uploads();
			exit();
		}elseif($action == 'del'){
			$this->del();
			exit();
		}else{
			$this->defaultAction();
			exit();
		}
	}

	function defaultAction(){		
		require_once(APP_PATH."model/admin/moviepic.php" );
		$db  = getDB();
		$data = new MoviepicModel ($db);
		if(is_numeric($this->url['params']['1'])){
			$movieid = $this->url['params']['1'];
			if($this->url['params']['2'] == 'page'){
				$pagetemp = $this->url['params']['3'];		
			}
			$condition['condition'] = " mid = '$movieid' ";
			$tpl = new Template( "admin/moviepic/movielist.htm" );
			$condition['pageUrl'] = '/admin/moviepic/'.$movieid.'/page/';
			$movie = $data->getMovie($movieid);

		}else{
			if($this->url['params']['1'] == 'page'){
				$pagetemp = $this->url['params']['2'];		
			}
			$tpl = new Template( "admin/moviepic/list.htm" );
			$condition['pageUrl'] = '/admin/moviepic/page/';
		}

		$condition['page']= is_numeric($pagetemp) ? $pagetemp :'1';
		$condition['rows'] = '20';
		$condition['order'] = 'addtime DESC';

		$photoList = $data->getList($condition);
		$tpl->assign('list', $photoList);

		$tpl->assign($movie);
		
		$condition['pageTotal']	= $data->total($condition);
		$tpl->assign("pageParam", makePage($condition));
		$tpl->output();
	}

	function edit(){
		require_once(APP_PATH."model/admin/moviepic.php" );
		$DB  = getDB();
		$data = new MoviepicModel ($DB);
		$id = $this->url['params']['2'];
		if(!is_numeric($id)){
			message('找不到此图片。');
		}
		$article = $data->getOne($id);
		if(!isPost()){
			$tpl = new Template("admin/moviepic/edit.htm" );
			$tpl->assign($article);
			$tpl->output();
		}else{			
			$title = trim($_POST['title']);	
			$edit = $data->editOne($id,$title);
			if ($edit){
				$message = '修改图片成功。';
			}else{
				$message = '修改图片失败！';
			}
			exit($message);			
		}
	}

	function del(){
		require_once(APP_PATH."model/admin/moviepic.php" );
		$db  = getDB();
		$data = new MoviepicModel( $db );
		$id = $this->url['params']['2'];
		if(!is_numeric($id)){
			message('找不到此条电影图片。');
		}
		$photo = $data->getOne($id);
		if(!$photo){
			message('找不到此条电影图片。');
		}
		$mid = $photo['mid'];
		$del = $data->delOne($id);
		if ($del){

			$sql = "UPDATE movie SET photos = photos - 1  WHERE id = '$mid' ";
			$db->query($sql);	

			$filename = $photo['filename'];
			$sourFile = UPLOAD_PATH.'photos/'.$filename;
			$targetFile = UPLOAD_PATH.'photos/thumb/'.$filename;
			fileDel($sourFile);
			fileDel($targetFile);
			$message = '删除电影图片成功。';
		}else{
			$message = '删除电影图片失败！';
		}
		message($message);
	}

	function uploads(){
		$url = getUrl();
		$mid = $url['params']['2'];
		if(!is_numeric($mid)){
			exit('system error');
		}
		if(!empty($_FILES['Filedata']['tmp_name'])){			
			require_once(CLASS_PATH."class/upload.class.php" );
			$upload = new Upload();
			$upload->config(array(
				'targetDir' => 'photos/',
				'saveType' => 2
			));
			$files = $upload->saveFiles('Filedata');
			$file_url = $files['0']['filenames'];
		}else{
			exit('2');
		}

		$sourFile = UPLOAD_PATH.'photos/'.$file_url;
		$targetFile = UPLOAD_PATH.'photos/thumb/'.$file_url;
		thumbs($sourFile,$targetFile,100,100);

		$db  = getDB();
		$time = time();
		$sql = "INSERT INTO movie_photo (mid,filename,addtime) VALUES ('$mid','$file_url','$time')";
		$db->query($sql);
		$sql = "UPDATE movie SET photos = photos+1  WHERE id = '$mid' ";
		$db->query($sql);	
		exit('ok');
	}

}
?>
